import { ResponseCodeEnum, useHttpClient } from 'pf-component';
import { systemApi } from '@/constants/api/system-api';
import { TNode } from '@/views/system/org/use-org';
import { useNotice } from '@/components/element-plus/notice';

export const useGuest = () => {
  const { message } = useNotice();

  const registerGuest = (body: any): Promise<boolean> => {
    body.userBirthday = `${body.userBirthday} 01:00:00`;
    return useHttpClient()
      .general<TNode[]>(systemApi.userApi.registerGuest, undefined, body)
      .then(res => {
        if (res.code !== ResponseCodeEnum.SUCCESS) {
          message.error(res.message);
        }
        return res.code === ResponseCodeEnum.SUCCESS;
      });
  };
  const listByGuest = (body: any): Promise<boolean> => {
    body.userBirthday = `${body.userBirthday} 01:00:00`;
    return useHttpClient()
      .general<TNode[]>(systemApi.userApi.listByGuest, undefined, body)
      .then(res => {
        if (res.code !== ResponseCodeEnum.SUCCESS) {
          message.error(res.message);
        }
        return res.code === ResponseCodeEnum.SUCCESS;
      });
  };
  const pageByOrg = (body: any): Promise<{ total: number; records: any[] }> => {
    return useHttpClient()
      .general(systemApi.userApi.pageByOrg, undefined, body)
      .then(res => {
        if (res.code !== ResponseCodeEnum.SUCCESS) {
          message.error(res.message);
          return { total: 0, records: [] };
        }
        return res.data as any;
      });
  };
  const pageGuest = (body: any): Promise<{ total: number; records: any[] }> => {
    return useHttpClient()
      .general(systemApi.userApi.pageGuest, undefined, body)
      .then(res => {
        if (res.code !== ResponseCodeEnum.SUCCESS) {
          message.error(res.message);
          return { total: 0, records: [] };
        }
        return res.data as any;
      });
  };
  const assignUser = (body: any): Promise<boolean> => {
    body.userBirthday = `${body.userBirthday} 01:00:00`;
    return useHttpClient()
      .general<TNode[]>(systemApi.userApi.assign, undefined, body)
      .then(res => {
        if (res.code !== ResponseCodeEnum.SUCCESS) {
          message.error(res.message);
        }
        return res.code === ResponseCodeEnum.SUCCESS;
      });
  };
  return {
    registerGuest,
    assignUser,
    pageGuest,
    pageByOrg,
    listByGuest
  };
};
